#include <iostream>
#include <condition_variable>
#include <vector>
#include <functional>
#include <thread>
#include <queue>
#include <mutex>
#include <atomic>

class Threadpool
{
    public:
        typedef std::function<void()> Task;

        Threadpool(int num);
        ~Threadpool();

        void finish();
        void addTask(const Task &t);
        void runTask();
        void setThreadPoolSize(int num);

    private:
        // 子线程调用. 暂时没有任务，让出CPU
        const bool is_empty(void);
        // 子线程调用. 任务全部完成, 线程被要求退出
        const bool all_done(void);

    private:
        std::vector<std::thread> threads;
        std::queue<Task> tasks;
        const int threadNum;
        bool done;

        std::mutex m_mutex;
        std::condition_variable m_cond;
};
